package com.wwj.auction.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwj.auction.admin.entity.Shop;
import com.wwj.auction.admin.entity.Store;
import com.wwj.auction.admin.entity.vo.PageModel;
import com.wwj.auction.admin.entity.vo.ShopVo;
import com.wwj.auction.admin.mapper.ShopMapper;
import com.wwj.auction.admin.mapper.StoreMapper;
import com.wwj.auction.admin.mapper.UserInfoMapper;
import com.wwj.auction.admin.service.ShopService;
import com.wwj.auction.common.util.SystemUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-08-29
 */
@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private StoreMapper storeMapper;

    @Override
    public PageModel<ShopVo> listForShop(Integer page, Integer limit) {
        // 查询商品数据
        Page<Shop> shopPage = new Page<>(page, limit);
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery(Shop.class);
        // 按照id降序
        wrapper.orderByDesc(Shop::getId);
        List<Shop> shopList = baseMapper.selectPage(shopPage, wrapper).getRecords();
        // 封装Vo数据
        List<ShopVo> shopVoList = shopList.stream().map(shop -> {
            ShopVo shopVo = new ShopVo();
            // 设置商品id
            shopVo.setId(shop.getId());
            shopVo.setShopName(shop.getShopName());
            shopVo.setShopIndexImage(shop.getShopIndexImage());
            // 查询商品所属的卖家
            Integer sellerId = shop.getSellerId();
            // 根据sellerId查询店铺信息
            Store store = storeMapper.selectOne(
                    Wrappers.lambdaQuery(Store.class)
                            .eq(Store::getUserId, sellerId));
            shopVo.setStoreName(store.getStoreName());
            shopVo.setShopStartPrice(shop.getShopStartPrice().toString());
            shopVo.setShopCurrentPrice(shop.getShopCurrentPrice().toString());
            shopVo.setShopEarnestPrice(shop.getEarnestMoney().toString());
            shopVo.setShopRetainPrice(shop.getShopRetainPrice().toString());
            shopVo.setStatus(shop.getStatus());
            // 设置开拍时间
            LocalDateTime startTime = shop.getStartTime();
            shopVo.setStartTime(SystemUtil.parseDate(startTime));
            LocalDateTime endTime = shop.getEndTime();
            shopVo.setEndTime(SystemUtil.parseDate(endTime));
            return shopVo;
        }).collect(Collectors.toList());
        // 封装最终数据
        PageModel<ShopVo> pageModel = new PageModel<>();
        pageModel.setList(shopVoList);
        pageModel.setTotal(shopPage.getTotal());
        return pageModel;
    }

    @Override
    public PageModel<ShopVo> listByStoreId(Integer storeId, Integer page, Integer limit) {
        // 通过店铺id查询卖家信息
        Store store = storeMapper.selectById(storeId);
        // 获取卖家id
        Integer userId = store.getUserId();
        // 通过卖家id查询所属的商品信息
        Page<Shop> shopPage = new Page<>(page, limit);
        LambdaQueryWrapper<Shop> wrapper = Wrappers.lambdaQuery(Shop.class)
                .eq(Shop::getSellerId, userId)
                .orderByDesc(Shop::getId);
        List<Shop> shopList = baseMapper.selectPage(shopPage, wrapper).getRecords();
        // 封装Vo数据
        List<ShopVo> shopVoList = shopList.stream().map(shop -> {
            ShopVo shopVo = new ShopVo();
            // 设置商品id
            shopVo.setId(shop.getId());
            shopVo.setShopName(shop.getShopName());
            shopVo.setShopIndexImage(shop.getShopIndexImage());
            // 查询商品所属的卖家
            Integer sellerId = shop.getSellerId();
            shopVo.setStoreName(store.getStoreName());
            shopVo.setShopStartPrice(shop.getShopStartPrice().toString());
            shopVo.setShopCurrentPrice(shop.getShopCurrentPrice().toString());
            shopVo.setShopEarnestPrice(shop.getEarnestMoney().toString());
            shopVo.setShopRetainPrice(shop.getShopRetainPrice().toString());
            shopVo.setStatus(shop.getStatus());
            // 设置开拍时间
            LocalDateTime startTime = shop.getStartTime();
            shopVo.setStartTime(SystemUtil.parseDate(startTime));
            LocalDateTime endTime = shop.getEndTime();
            shopVo.setEndTime(SystemUtil.parseDate(endTime));
            return shopVo;
        }).collect(Collectors.toList());
        // 封装最终数据
        PageModel<ShopVo> pageModel = new PageModel<>();
        pageModel.setList(shopVoList);
        pageModel.setTotal(shopPage.getTotal());
        return pageModel;
    }

    @Override
    public Store getStoreName(Integer shopId) {
        Shop shop = baseMapper.selectOne(
                Wrappers.lambdaQuery(Shop.class)
                        .eq(Shop::getId, shopId));
        // 得到店铺id
        Integer storeId = shop.getStoreId();
        // 通过店铺id查询店铺信息
        Store store = storeMapper.selectById(storeId);
        return store;
    }
}
